package com.smag.web.controller.system;

import com.smag.common.annotation.Log;
import com.smag.common.config.RuoYiConfig;
import com.smag.common.core.controller.BaseController;
import com.smag.common.core.domain.AjaxResult;
import com.smag.common.core.domain.entity.FarmTech;
import com.smag.common.core.domain.entity.SysUser;
import com.smag.common.core.page.TableDataInfo;
import com.smag.common.enums.BusinessType;
import com.smag.common.utils.SecurityUtils;
import com.smag.common.utils.StringUtils;
import com.smag.common.utils.file.FileUploadUtils;
import com.smag.common.utils.file.MimeTypeUtils;
import com.smag.common.utils.poi.ExcelUtil;
import com.smag.system.service.FarmTechService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/system/tech")
public class SysTechController extends BaseController {
    @Resource
    private FarmTechService farmTechService;

    @PreAuthorize("@ss.hasPermi('system:tech:list')")
    @GetMapping("/list")
    public TableDataInfo pageListCrop(FarmTech farmTech){
        startPage();
        if (SysUser.isAdmin(SecurityUtils.getUserId()))
        return getDataTable(farmTechService.pageListCrop(farmTech,null));
        return getDataTable(farmTechService.pageListCrop(farmTech,SecurityUtils.getUserId()));
    }
    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FarmTech farmTech)
    {
        List<FarmTech> list = farmTechService.list();
        ExcelUtil<FarmTech> util = new ExcelUtil<FarmTech>(FarmTech.class);
        util.exportExcel(response, list, "农业技术数据");
    }
    @DeleteMapping("/{cropIds}")
    @PreAuthorize("@ss.hasPermi('system:tech:remove')")
    @Log(title = "农业技术管理", businessType = BusinessType.DELETE)
    public AjaxResult remove(@PathVariable Long[] cropIds) {
        return farmTechService.deleteCropByIds(cropIds);
    }

    @PutMapping
    @PreAuthorize("@ss.hasPermi('system:tech:edit')")
    @Log(title = "农业技术管理", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@Validated @RequestBody FarmTech farmTech) {
        if (StringUtils.isNotEmpty(farmTech.getAvatar()))
            farmTech.setAvatar(null);
        return farmTechService.updateCrop(farmTech);
    }
    @PostMapping
    @PreAuthorize("@ss.hasPermi('system:tech:add')")
    @Log(title = "农业技术管理", businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestBody FarmTech farmTech){
        return farmTechService.insertCrop(farmTech);
    }

    @PostMapping("avatar")
    @Log(title = "农业技术管理", businessType = BusinessType.EXPORT)
    public AjaxResult avatar(@RequestParam("cropId") Long cropId,@RequestParam("avatarfile")MultipartFile file) throws Exception {
        if (!file.isEmpty()){
            String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            if (farmTechService.updateTechAvatar(cropId,avatar)) {
                AjaxResult ajax = AjaxResult.success();
                ajax.put("imgUrl", avatar);
                return ajax;
            }else{
                return error("上传图片失败，请联系管理员");
            }
        }
        return error("上传图片异常，请联系管理员");
    }

}
